package com.global.emc.spring.chain.command.token;

import javax.servlet.http.HttpServletRequest;

import com.global.emc.core.components.token.ResubmitToken;
import com.global.emc.core.components.token.Token;
import com.global.emc.core.components.token.TokenManager;
import com.global.emc.core.components.token.impl.RandomNumberTokenManager;
import com.global.emc.core.exception.impl.EMCException;
import com.global.emc.core.exception.impl.ValidationEMCException;
import com.global.emc.spring.chain.BeforeCallMethodContent;
import com.global.emc.spring.chain.command.AbstractBeforeCallMethodCommand;

/**   
 *  
 * @Description:  [防重复提交token AbstractBeforeCommand]   
 * @Author:       [Joly_Huang]   
 * @CreateDate:   [2014年1月30日 下午2:37:28]   
 * @UpdateUser:   [Joly_Huang]   
 * @UpdateDate:   [2014年1月30日 下午2:37:28]   
 * @UpdateRemark: [说明本次修改内容]  
 * @Version:      [v1.0] 
 *    
 */
public class ResubmitTokenBCMCommand extends AbstractBeforeCallMethodCommand {
	
	private TokenManager tokenManager;
	
	
	public TokenManager getTokenManager() {
		return tokenManager;
	}
	public void setTokenManager(TokenManager tokenManager) {
		this.tokenManager = tokenManager;
	}

	@Override
	public void execute(BeforeCallMethodContent content) throws EMCException {
		ResubmitToken resubmitToken = super.findObject(content, ResubmitToken.class);
		
		String cacheTokenName = ((RandomNumberTokenManager)tokenManager).getTokenName();
		
		HttpServletRequest servletRequest = content.getRequest().getNativeRequest(HttpServletRequest.class);
		Token cacheToken = (Token)servletRequest.getSession().getAttribute(cacheTokenName);
		
		if(null == cacheToken)
			throw new ValidationEMCException("cache token is null");

		tokenManager.verifyToken(resubmitToken.getTokenUniqueId(), cacheToken);
		
	}
	
}
